package com.oceansoft.mobile.econsole.modules.weixin.controller;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.common.util.StringUtils;
import com.oceansoft.mobile.econsole.common.util.encrypt.DesAPPUtils;
import com.oceansoft.mobile.econsole.modules.api.entity.CarType;
import com.oceansoft.mobile.econsole.modules.api.service.IQueryService;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.member.entity.WtMember;
import com.oceansoft.mobile.econsole.modules.member.service.IMemberService;
import com.oceansoft.mobile.econsole.modules.member.service.IWtMemberService;
import com.oceansoft.mobile.econsole.modules.weixin.config.WxFactory;
import com.oceansoft.mobile.econsole.weixin.api.OauthAPI;
import com.oceansoft.mobile.econsole.weixin.api.enums.OauthScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 便民服务 Convenient Service
 *
 * @author jin.xu
 * @createTime 2015年4月13日 下午2:24:45
 * @packagePath com.oceansoft.mobile.econsole.modules.weixin.controller.BmServiceController.java
 */
@Controller
@RequestMapping("weixin/bm")
public class BmServiceController extends BaseController {

    @Resource
    private IMemberService memberService;

    @Resource
    private IQueryService queryService;
    @Resource
    private IWtMemberService wtMemberService;
    /**
     * 测试页面
     */
    @RequestMapping(value = "index", method = GET)
    public String gotoIndex() {
        return "weixin/index";
    }


    /**
     * 便民地图
     *
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "loadmap", method = GET)
    public String loadMap(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        String searchName = request.getParameter("searchname");
        String searchID = request.getParameter("searchid");
        String mapLng = request.getParameter("maplng");
        String mapLat = request.getParameter("maplat");
        if (StringUtils.isNotEmpty(searchName))
            searchName = URLDecoder.decode(searchName, "UTF-8");
        model.addAttribute("searchname", searchName);
        model.addAttribute("searchid", searchID);
        model.addAttribute("maplng", mapLng);
        model.addAttribute("maplat", mapLat);
        return "weixin/bmsrv/map";
    }

    /**
     * 便民地图路线查询结果
     *
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "routeinfo/{startpoi}/{endpoi}", method = GET)
    public String gotoRouteResult(@PathVariable("startpoi") String startPoi, @PathVariable("endpoi") String endPoi, Model model) throws UnsupportedEncodingException {
        startPoi = URLDecoder.decode(startPoi, "UTF-8");
        endPoi = URLDecoder.decode(endPoi, "UTF-8");
        model.addAttribute("startpoi", startPoi);
        model.addAttribute("endpoi", endPoi);
        return "weixin/bmsrv/route_info";
    }

    /**
     * 堵路移车oauth认证
     */
    @RequestMapping(value = "dlyc/oauth", method = GET)
    public String gotodlycOAuth(HttpServletRequest request) {
        OauthAPI oauthAPI = WxFactory.buildOauthAPI();
        return "redirect:" + oauthAPI.getOauthPageUrl(WxFactory.DLYC_OAUTH_URL, OauthScope.SNSAPI_BASE, "123");
    }

    /**
     * 便民服务页面跳转
     */
    @RequestMapping(value = "dlyc", method = GET)
    public String gotoPage(HttpServletRequest request,Model model) throws Exception{
       /* WxMember wxMember = getWxMember(request);

        Result result = memberService.openIdAuth(wxMember.getOpenId());
        if(result.isSucc()){
            saveWxMemberSession((Member)result.getData());
        }

        model.addAttribute("wxMember",wxMember);
        model.addAttribute("openId",wxMember.getOpenId());*/
        WtMember wtMember = getWxMemberSession();

        if(!Strings.isNullOrEmpty(wtMember.getUserCode()) && !com.oceansoft.mobile.econsole.modules.weixin.config.Constant.isIDCard(wtMember.getUserCode())){
            String userCode = DesAPPUtils.decrypt(wtMember.getUserCode());
            wtMember.setUserCode(userCode);
        }

        if(!Strings.isNullOrEmpty(wtMember.getMobile()) && !com.oceansoft.mobile.econsole.modules.weixin.config.Constant.isMobile(wtMember.getMobile())){
            String mobile = DesAPPUtils.decrypt(wtMember.getMobile());
            wtMember.setMobile(mobile);
        }

        model.addAttribute("member",getWxMemberSession());
        model.addAttribute("pushId",request.getParameter("pushId"));
        model.addAttribute("pushType",request.getParameter("pushType"));
        // 获取车辆类型
        Map<String,String> map = (Map<String,String>)request.getSession().getServletContext().getAttribute(Constant.DICT_CAR_TYPE);
        if(map.size() > 0){
            List<CarType> list = new ArrayList<>();
            for(String key : map.keySet()){
                CarType carType = new CarType();
                carType.setCode(key);
                carType.setName(map.get(key));
                list.add(carType);
            }
            model.addAttribute("list",list);
        }

        return "weixin/bmsrv/dlyc";
    }

    /**
     * 重名查询页面跳转
     */
    @RequestMapping(value = "cmcx", method = GET)
    public String gotoCMCXPage(HttpServletRequest request,Model model) throws Exception{
        String pt = request.getParameter("pt") == null ? "" : request.getParameter("pt").toString();
        model.addAttribute("pushId",request.getParameter("pushId"));
        model.addAttribute("pushType",request.getParameter("pushType"));
        model.addAttribute("pt",pt);
        return "weixin/bmsrv/cmcx";
    }

    /**
     * 违法查询页面跳转
     */
    @RequestMapping(value = "wfcx/jdcwf", method = GET)
    public String gotoWFCXJDCWFPage(HttpServletRequest request,Model model) throws Exception{

//        String pt = request.getParameter("pt") == null ? "" : request.getParameter("pt").toString();
//
        List<CarType> carTypelist = (List<CarType>)queryService.loadWtCarTypeList(Constant.JDCHPZL).getData();
//
        model.addAttribute("carTypelist",carTypelist);
//        model.addAttribute("pushId",request.getParameter("pushId"));
//        model.addAttribute("pushType",request.getParameter("pushType"));
//        model.addAttribute("pt",pt);

        return "weixin/bmsrv/wfcx_jdcwf";
    }

    /**
     * 车辆查询页面跳转
     */
    @RequestMapping(value = "clcx", method = GET)
    public String gotoCLCX(HttpServletRequest request,Model model) throws Exception {
        List<CarType> carTypelist = (List<CarType>)queryService.loadWtCarTypeList(Constant.JDCHPZL).getData();

        model.addAttribute("carTypelist",carTypelist);
        return "weixin/bmsrv/clcx";
    }
        /**
     * 驾驶证积分查询页面跳转
     */
    @RequestMapping(value = "wfcx/jszjf", method = GET)
    public String gotoWFCXJSZJFPage(HttpServletRequest request,Model model) throws Exception{

//        model.addAttribute("pushId",request.getParameter("pushId"));
//        model.addAttribute("pushType",request.getParameter("pushType"));

        return "weixin/bmsrv/wfcx_jszjf";
    }
    /**
     *案件查询
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "ajcx", method = GET)
    public String gotoAJCX(HttpServletRequest request,Model model) throws Exception{

        return "weixin/bmsrv/ajcx";
    }
    /**
     * 路况信息H5页面调整
     *
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "jtlkmap", method = GET)
    public String loadjtlkMap(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        return "weixin/map/TXMap";
    }

    /**
     * 身份证办理进度查询页面跳转
     */
    @RequestMapping(value = "sfzbl", method = GET)
    public String gotoSFZBLPage(HttpServletRequest request,Model model) throws Exception{

        String platform = request.getParameter("platform");
        if(StringUtils.isNotBlank(platform)){
            model.addAttribute("platform",platform);
        }

        return "weixin/bmsrv/sfzbl";
    }

    /**
     * 保安员查询页面跳转
     */
    @RequestMapping(value = "baycx", method = GET)
    public String gotoBAYCXPage(HttpServletRequest request,Model model) throws Exception{

        String platform = request.getParameter("platform");
        if(StringUtils.isNotBlank(platform)){
            model.addAttribute("platform",platform);
        }

        return "weixin/bmsrv/baycx";
    }

    /**
     * 社区移动微警务用户须知页面跳转
     */
    @RequestMapping(value = "notice", method = GET)
    public String gotoSQMJXZ(HttpServletRequest request) throws Exception{
        WtMember wtMember;
        String userGuid = request.getParameter("userGuid");
        if(Strings.isNullOrEmpty(userGuid)){
            wtMember = getWxMemberSession();
        }else {
            wtMember = wtMemberService.getWtMemberByUserGuid(userGuid);
        }
        Preconditions.checkNotNull(wtMember);
        this.saveWxMemberSession(wtMember);
        return "weixin/bmsrv/communityPolice-notice";
    }
    /**
     * 社区移动微警务页面跳转
     */
    @RequestMapping(value = "gotoSearch", method = GET)
    public String gotoSQMJ(HttpServletRequest request,Model model) throws Exception{
        model.addAttribute("methodType",this.getDicts(Constant.DICT_SEARCH_METHOD));
        return "weixin/bmsrv/communityPolice-search";
    }

    /**
     * 网上办件查询页面跳转
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "gotofile", method = GET)
    public String gotoBJCX(HttpServletRequest request,Model model) throws Exception{
        model.addAttribute("methodType",this.getDicts(Constant.DICT_SEARCH_METHOD));
        return "weixin/bmsrv/bjcx";
    }

}
